﻿Partial Public Class Form1
    Private LevelActiveBackColor() As Color = {Color.Black, Color.DarkBlue, Color.DarkMagenta, Color.DarkGreen, Color.DarkOrange, Color.DarkCyan}

    Private LevelActiveForeColor() As Color = {Color.White, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White}

    Private SubLevelActiveBackColor() As Color = {Color.LightGray, Color.LightBlue, Color.LightCoral, Color.LightGreen, Color.LightYellow, Color.LightCyan}

    Private SubLevelActiveForeColor() As Color = {Color.Black, Color.Black, Color.Black, Color.Black, Color.Black, Color.Black, Color.Black, Color.Black, Color.Black, Color.Black, Color.Black, Color.Black}

    'Private LevelInActiveBackColor() As Color = {Color.Black, Color.DarkBlue, Color.DarkMagenta, Color.DarkGreen, Color.DarkRed, Color.DarkCyan, Color.DarkGray, Color.Blue, Color.Green, Color.Magenta, Color.Red, Color.Cyan}
    'Private LevelInActiveForeColor() As Color = {Color.White, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White}



    Private DriveActiveBackColor As Color = Color.Black
    Private DriveActiveForeColor As Color = Color.Yellow
    Private DriveInActiveBackColor As Color = Color.Gray
    Private DriveInActiveForeColor As Color = Color.Yellow

    Private ColorBackUninitiated As Color = Color.White
    Private ColorForeUninitiated As Color = Color.Black

    Dim varMode As Modes
    Enum Modes
        Drives
        Folders
        All
        Debug
    End Enum
    Private Sub SetMode(mode As Modes)
        varMode = mode
        Dim HideTheseForAll As List(Of String) = New List(Of String) From {"RowState", "ParentPath", "ParentIndex", "DirIndex", "RowTag"}
        Dim HideTheseForFolders As List(Of String) = New List(Of String) From {"VolumeLabel", "DriveSize", "FreeSpace", "PercentFreeSpace", "DriveFormat"}
        Dim HideTheseForDrives As List(Of String) = New List(Of String) From {"Directory", "StorageSize", "DirectoryCount", "FileCount", "TotalDirCount", "TotalFileCount"}
        For Each col As DataGridViewColumn In DataGridView1.Columns()
            col.Visible = True
            If HideTheseForAll.Contains(col.Name) And Not (varMode = Modes.Debug) Then
                col.Visible = False
            Else
                If ((varMode = Modes.Folders) And (HideTheseForFolders.Contains(col.Name))) Then
                    col.Visible = False
                ElseIf ((varMode = Modes.Drives) And (HideTheseForDrives.Contains(col.Name))) Then
                    col.Visible = False
                End If
            End If
        Next
    End Sub

    Private Sub DataGridView1_Sort()
        DataGridView1.Sort(New RowComparer(Windows.Forms.SortOrder.Ascending))
    End Sub


    'Ref: http://tedshelloworld.blogspot.com.au/2008/05/datagridview-sorting-numeric-columns.html
    'Private Sub DataGridView1_SortCompare(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewSortCompareEventArgs) Handles DataGridView1.SortCompare
    '    If Double.Parse(e.CellValue1) > Double.Parse(e.CellValue2) Then
    '        e.SortResult = 1
    '    ElseIf Double.Parse(e.CellValue1) < Double.Parse(e.CellValue2) Then
    '        e.SortResult = -1
    '    Else
    '        e.SortResult = 0
    '    End If
    '    e.Handled = True
    '    Exit Sub
    'End Sub

    'Ref: http://msdn.microsoft.com/en-us/library/ms171608.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-3
    Private Class RowComparer
        Implements System.Collections.IComparer

        Private sortOrderModifier As Integer = 1

        Public Sub New(ByVal xsortOrder As Windows.Forms.SortOrder)
            If xsortOrder = Windows.Forms.SortOrder.Descending Then
                sortOrderModifier = -1
            ElseIf xsortOrder = Windows.Forms.SortOrder.Ascending Then

                sortOrderModifier = 1
            End If
        End Sub

        Public Function DataGridView1_Compare(ByVal x As Object, ByVal y As Object) As Integer _
            Implements System.Collections.IComparer.Compare

            Try
                Dim DataGridView1Row1 As DataGridViewRow = CType(x, DataGridViewRow)
                Dim DataGridView1Row2 As DataGridViewRow = CType(y, DataGridViewRow)

                'DataGridView1Row1.Cells("RowTag").Value is of format LL.II,   <repeatedly> where LL is a level number and II is an index
                ' eg 0.1,1.7,2.3  means First level(0)  second drive(1), second level (1) eight folder(7), third level(3) fourth folder(3)

                Dim tag1 As String = DataGridView1Row1.Cells("RowTag").Value
                Dim tags1() As String = tag1.Split(",")
                Dim tags1Max As Integer = tags1.Count

                Dim tag2 As String = DataGridView1Row2.Cells("RowTag").Value
                Dim tags2() As String = tag2.Split(",")
                Dim tags2Max As Integer = tags2.Count

                Dim maxTag As Integer = tags1Max
                If (tags2Max > tags1Max) Then maxTag = tags2Max

                Dim CompareResult As Integer = 0  '0 means 2 is larger 1 means 1 is larger

                Dim dbl1 As Double
                Dim dbl2 As Double

                For i As Integer = 0 To maxTag - 1

                    If (i > tags1Max - 1) Then
                        'We are done with 1 so it must be higher level so it comes first
                        CompareResult = -1
                        Exit For
                    ElseIf (i > tags2Max - 1) Then
                        'We are done with 2 so it must be higher level so it comes first
                        CompareResult = 1
                        Exit For
                    End If

                    dbl1 = CDbl(Trim(tags1(i)))
                    dbl2 = CDbl(Trim(tags2(i)))

                    If (dbl1 = dbl2) Then
                        'If the tags at this level are the same then go to next level
                        Continue For
                    Else
                        'Otherwise there must be an inequality
                        If dbl1 > dbl2 Then
                            CompareResult = 1
                            Exit For
                        Else
                            CompareResult = -1
                            Exit For
                        End If
                    End If

                Next


                CompareResult *= sortOrderModifier

                Return CompareResult

            Catch ex As Exception
                Dim i As Integer = 0
            End Try
            Return -1
        End Function
    End Class


End Class
